package com.zmt.mrms.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zmt.mrms.entity.HospitalListPrice;
import com.zmt.mrms.entity.HospitalSection;
import com.zmt.mrms.entity.MedicalRecord;
import com.zmt.mrms.entity.PaymentRecord;
import com.zmt.mrms.entity.PrescriptionRecord;
import com.zmt.mrms.entity.UserPatient;

/**
 * 1. 添加了@Mapper注解之后这个接口在编译时会生成相应的实现类 需要注意的是：这个接口中不可以定义同名的方法，因为会生成相同的id
 * 也就是说这个接口是不支持重载的 2. 对于多个参数来说，每个参数之前都要加上@Param注解， 要不然会找不到对应的参数进而报错
 */

@Mapper
public interface PatientMapper {

	// 处方查询模块
	@Select("select * from prescription_record where medical_record_id=#{medRecordId}")
	PrescriptionRecord findPreRecordByMedRecId(@Param("medRecordId") int medRecordId);

	// 病历查询模块
	@Select("select * from medical_record where id=#{id}")
	MedicalRecord findRecordByMedicalId(@Param("id") int id);

	@Select("select * from medical_record where patient_idcard=#{idcard}")
	List<MedicalRecord> findRecordByIdcard(@Param("idcard") String idcard);

	@Select("select * from medical_record where patient_id=#{id}")
	List<MedicalRecord> findRecordByPatientId(@Param("id") int id);

	// 付款模块
	@Update("update payment_record set ${billType}=true where id=#{paymentId}")
	int payBill(@Param("billType") String billType, @Param("paymentId") int paymentId);

	@Select("select * from payment_record where id=#{id}")
	PaymentRecord getPayment(@Param("id") int id);

	@Insert("insert into payment_record(medical_record_id,registered_cost,registered_status) values(#{MedicalRecordId},#{registeredCost},#{registeredStatus})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	void createPaymentRecord(PaymentRecord paymentRecord);

	@Select("select * from hospital_list_price where name=#{name}")
	HospitalListPrice registeredPrice(@Param("name") String name);

	// 挂号模块
	@Insert("insert into medical_record(patient_id,patient_name,patient_sex,patient_age,patient_idcard,registered_type,registered_section,description,create_time) "
			+ "values(#{patientId},#{patientName},#{patientSex},#{patientAge},#{patientIdcard},#{registeredType},#{registeredSection},#{description},#{createTime})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	void createMedicalRecord(MedicalRecord medicalRecord);

	@Select("select * from user_patient where identify_card = #{idcard}")
	UserPatient findbyIDCard(@Param("idcard") String idcard);

	@Select("select * from hospital_section")
	List<HospitalSection> selectAllSection();

	// 个人信息模块
	@Update("update user_patient set name=#{name},birthday=#{birthday},sex=#{sex},identify_card=#{identifyCard},medical_card=#{medicalCard},address=#{address} where id=#{id}")
	int updatePersonalInfo(UserPatient userPatient);

	@Update("update user_patient set email=#{email},password=#{password},phone=#{phone} where id=#{id}")
	int updateAccountInfo(UserPatient userPatient);

	@Select("select * from user_patient where id = #{id}")
	UserPatient findbyId(@Param("id") int id);

	@Select("select * from user_patient where email = #{email}")
	UserPatient findbyEmail(@Param("email") String email);

	// 用户注册
	@Insert("insert into user_patient(email,password,role) values(#{email},#{pwd},#{role})")
	int createAccountbyEmail(@Param("email") String email, @Param("pwd") String pwd, @Param("role") String role);

	// 用户登录
	@Select("select * from user_patient where email = #{email} and password = #{pwd}")
	UserPatient loginbyEmail(@Param("email") String email, @Param("pwd") String pwd);

	@Select("select * from user_patient where phone = #{phone} and password = #{pwd}")
	UserPatient loginbyPhone(@Param("phone") String phone, @Param("pwd") String pwd);

}
